package org.jeecg.modules.zonglzp.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.FinanceModule.service.MonthStatementsService;
import org.jeecg.modules.zonglzp.entity.BusinesswaterEntity;
import org.jeecg.modules.zonglzp.entity.store;
import org.jeecg.modules.zonglzp.service.BusinesswaterService;
import org.jeecg.modules.zonglzp.service.CompanyService;
import org.jeecg.modules.zonglzp.service.orderAfterService;
import org.jeecg.modules.zonglzp.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Businesswater")
public class BusinesswaterController {

    @Autowired
    orderAfterService orderAfterService;

    @Autowired
    private CompanyService companyservice;
    @Autowired
    private BusinesswaterService service;

    @Autowired
    MonthStatementsService monthStatementsService;
    /*
    账号充值页面查询该人 账号余额 月结额度 今日消费
     */
    @GetMapping("/moneydetail")
    public JSONObject moneydetail(
            @RequestParam(name = "token") String token
    ) {
        JSONObject obj = new JSONObject();
        String userid = companyservice.getUseridbytoken(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }

        Map<String, Object> moneymap = new HashMap<>();
        moneymap = service.getmoneydetail(token);

        obj.put("code", 0);
        obj.put("data", moneymap);

        return obj;
    }


    /*
    账号充值页面某日明细
     */
    @GetMapping("/getonedaydetail")
    public JSONObject onedaydetail(
            @RequestParam(name = "token") String token,
            String today,
            String starttime, //开始时间
            String endtime,  //结束时间
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "index", defaultValue = "0") int index
    ) {
        DateUtils dateUtils = new DateUtils();

        if (index == 1) {
            today = dateUtils.getToday();
        } else {
            today = dateUtils.getYestoday();
        }


        JSONObject obj = new JSONObject();
        //通过用户token获取用户id
        String userid = companyservice.getUseridbytoken(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }

        Page<BusinesswaterEntity> page = new Page<BusinesswaterEntity>(pageNo, pageSize);
        IPage<BusinesswaterEntity> pageList = service.getonedaydetail(page, userid, today, starttime, endtime);
        //成交笔数
        int transactions = pageList.getRecords().size();
        int RefundAmount = 0;
        int SumAmount = 0;

        for (int i = 0; i < pageList.getRecords().size(); i++) {
            RefundAmount = RefundAmount + Integer.parseInt(pageList.getRecords().get(i).getReturnMoney());
            SumAmount = SumAmount + Integer.parseInt(pageList.getRecords().get(i).getCostMoney());
        }

        if (pageList.getRecords().size() > 0) {
            obj.put("code", 0);
            obj.put("data", pageList);
            obj.put("transactions", transactions);
            obj.put("RefundAmount", RefundAmount);
            obj.put("SumAmount", SumAmount);
        } else {
            obj.put("code", 0);
            obj.put("data", "未查到数据");
        }
        return obj;
    }


    /*
    报表页面
    查询这个人一个区间内的流水,
     */
    @GetMapping("/getBusinesswater")
    public JSONObject getBusinesswater(
            @RequestParam(name = "token") String token,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String starttime, //开始时间
            String endtime,  //结束时间
            String storeCode, //门店编号
             String storeId
    ) {
        if("".equals(storeId)){
            storeId=null;
        }
        //ELEMENTUI 的时间组件如果不选 默认会传空格进来
        if ("".equals(starttime))
            starttime = "1949-10-01";
        if ("".equals(endtime))
            endtime = "2099-12-20";



        if (endtime != null) {
            endtime = addDay(endtime, 1);
        }



        JSONObject obj = new JSONObject();
        //通过用户token获取用户id
        String userid = companyservice.getUseridbytoken(token);

        //通过token查询权限代码
        String orgCode=orderAfterService.getuserorgCode(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }
        //通过用户id查询该用户的报表(业务流水表)
        Page<BusinesswaterEntity> page = new Page<BusinesswaterEntity>(pageNo, pageSize);
        IPage<BusinesswaterEntity> pageList = service.getBusinesswater(page, starttime, endtime, storeCode, userid,orgCode,storeId);

        if (pageList.getRecords().size() > 0) {
            obj.put("code", 0);
            obj.put("data", pageList);
        } else {
            obj.put("code", 0);
            obj.put("data", "未查到数据");
        }
        return obj;
    }


    /*
    已完成单据和未完成单据
     */
    @GetMapping("/getbill")
    public JSONObject getwclist(
            @RequestParam(name = "token") String token,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String starttime, //开始时间
            String endtime,  //结束时间
            String storeCode, //门店编号
            String storeId,
            int ddstatus //0代表未完成 2代表完成 3退款

    ) {
        if("".equals(storeId)){
            storeId=null;
        }
        //ELEMENTUI 的时间组件如果不选 默认会传空格进来
        if ("".equals(starttime))
            starttime = "1949-10-01";
        if ("".equals(endtime))
            endtime = "2099-12-20";

        if (endtime != null) {
            endtime = addDay(endtime, 1);
        }


        JSONObject obj = new JSONObject();
        //通过用户token获取用户id
        String userid = companyservice.getUseridbytoken(token);
        //通过token查询权限代码
        String orgCode=orderAfterService.getuserorgCode(token);
        System.out.println("打印看一看"+orgCode);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }
        Page<BusinesswaterEntity> page = new Page<BusinesswaterEntity>(pageNo, pageSize);
        IPage<BusinesswaterEntity> pageList = service.getBusinesswater(page, starttime, endtime, storeCode, userid, ddstatus,orgCode,storeId);
        Double ywcMoney = service.moneyStatistics(starttime, endtime, storeCode, userid, 2,orgCode,storeId);
        if (ywcMoney == null) {
            ywcMoney = 0.0;
        }
        Double wwcMoney = service.moneyStatistics(starttime, endtime, storeCode, userid, 0, orgCode,storeId);
        if (wwcMoney == null) {
            wwcMoney = 0.0;
        }
        Double refundMoney=service.moneyStatistics(starttime, endtime, storeCode, userid, 3, orgCode,storeId);
        if (refundMoney == null) {
            refundMoney = 0.0;
        }



        if (pageList != null) {
            for (int i = 0; i < pageList.getRecords().size(); i++) {
                pageList.getRecords().get(i).setCreateTime(pageList.getRecords().get(i).getCreateTime().substring(0, 19));
            }
        }

        if (pageList != null) {
            obj.put("code", 0);
            obj.put("data", pageList);
            obj.put("ywcMoney", ywcMoney);
            obj.put("wwcMoney", wwcMoney);
            obj.put("refundMoney", refundMoney);

        } else {
            obj.put("code", 0);
            obj.put("data", "未查到数据");
            obj.put("ywcMoney", ywcMoney);
            obj.put("wwcMoney", wwcMoney);
        }

        return obj;
    }


    //时间增加一天方法
    public static String addDay(String s, int n) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cd = Calendar.getInstance();
            cd.setTime(sdf.parse(s));
            cd.add(Calendar.DATE, n);//增加一天
            //cd.add(Calendar.MONTH, n);//增加一个月

            return sdf.format(cd.getTime());

        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 根据团订编号查询门店详情
     */
    @GetMapping("/getstoreDetailByorderSn")
    public JSONObject getstoreDetailByorderSn(
            @RequestParam(name = "orderSn") String orderSn
    ){
        JSONObject obj=new JSONObject();

        store entity=service.getstoreDetailByorderSn(orderSn);
        if(entity==null){
            obj.put("code",1);

        }else {
            obj.put("code",0);
            obj.put("data",entity);
        }
        return obj;
    }


    /**
     * 查询公司下所有门店 以list返回
     */

    @GetMapping("/getstrorelist")
    public JSONObject getstrorelist(
            @RequestParam(name = "token") String token
    ){

        JSONObject obj = new JSONObject();

        //通过token查询权限代码
        String orgCode=orderAfterService.getuserorgCode(token);
        List<store> list=monthStatementsService.getstrorelist(orgCode);
        obj.put("data", list);
        obj.put("code", 0);
        return obj;

    }
}
